# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""A simple user signup page."""

from dzapp import *
from dzutil import link_to, redirect_to
from dzpage import Page


class SignupApp(App):

    def __init__(self):
        App.__init__(self)
        self.iconname = 'info'

    def _view_(self):
        if zoomer.admin_group in zoomer.user_groups:
            address_list = self.__addresslist()
            page = self.__content(address_list)
            return page
        else:
            return redirect_to(url=zoomer.main_url)

    def _search_response(self,**kwargs):
        if zoomer.admin_group in zoomer.user_groups:
            address_list = self.__addresslist(kwargs.get('search_text'))
            page = self.__content(address_list)
            return page
        else:
            return Redirect(zoomer.main_url)

    def __content(self,address_list):
        page = Page(address_list)
        page.title = 'People who have Signed-up'
        if not address_list:
            page.message = 'Unfortunately, nobody has signed up yet%s!' % (zoomer.webvars.search_text and ' with the name "%s"' % zoomer.webvars.search_text or '')
#         formcontent = Composite([Html('<input type=text size=18 name=search_text value="%s">' % zoomer.webvars.search_text),
#                 Button('Search')])
#         form = Form('search_response',formcontent)
#         page.parts = SidePanel('Search Signups',form)
        return page

    def __addresslist(self,search=None):
        try:
            f = open('addresses.txt','r')
        except:
            address_list = ''
        else:
            if search==None: address_list = ''.join(['%s<br>' % addr for addr in f.readlines()])
            else: address_list = ''.join(['%s<br>' % addr for addr in f.readlines() if addr.find(search)<>-1])
            f.close
        return address_list

    def _signup(self,email):
        if email:
            f = open('addresses.txt','a')
            f.write('%s\n' % email)
            f.close()
            page = Page("""<div class="widebox">
<br>
<br>
<br>
</div>
<br>
<center>
Your e-mail address has been added.<br><br>

<b>Thanks for signing up!<br><br>

We can't invite everyone at once but as soon<br> as there's an opening we'll let you know.</b><br><br>

%s
</center>
""" % link_to('Take me back',app=''))
            page.title = ''
            return page

result = SignupApp()

if __name__ == '__main__':
    app = SignupApp()
